package com.gufe.service.admin.impl.datascreen;

import com.gufe.exception.NullDataException;
import com.gufe.mapper.admin.datascreen.LeftChartMapper;
import com.gufe.service.admin.datascreen.LeftChartService;
import com.gufe.vo.datascreen.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class LeftChartServiceImpl implements LeftChartService {

    @Autowired
    LeftChartMapper leftChartMapper;

    @Override
    public List<LeftTopChartVO> getLeftTopChartData() {
        //获取预储,社团人数，多表联查
        List<LeftTopChartVO> list = leftChartMapper.getLeftTopChartData();
        return list;
    }

    @Override
    public LeftCenterChartVO getLeftCenterChartData(Integer[] ages) {
        if (ages == null || ages.length == 0) {
            throw new NullDataException();
        }
        //获取预储成员数据
        List<Integer> preReserveList = leftChartMapper.getPreReserveByAge(ages);
        //获取社团成员数据
        List<Integer> clubList = leftChartMapper.getClubByAge(ages);
        return new LeftCenterChartVO(preReserveList, clubList);
    }

    @Override
    public LeftBottomChartVO getLeftBottomChartData() {
        return new LeftBottomChartVO(
                // 确保所有区间都有数据（包含空区间）
                fillEmptyRanges(leftChartMapper.getManHeight(),
                        Arrays.asList("160以下", "160-170", "170-180", "180以上")),

                fillEmptyRanges(leftChartMapper.getWomanHeight(),
                        Arrays.asList("158以下", "158-168", "168-178", "178以上")),

                fillEmptyRanges(leftChartMapper.getWeight(),
                        Arrays.asList("50以下", "50-60", "60-70", "70以上"))
        );
    }
    // 填充空区间工具方法
    private List<PieChartVO> fillEmptyRanges(List<PieChartVO> data, List<String> allRanges) {
        Map<String, Integer> dataMap = data.stream()
                .collect(Collectors.toMap(PieChartVO::getName, PieChartVO::getValue));

        return allRanges.stream()
                .map(range -> new PieChartVO(dataMap.getOrDefault(range, 0), range))
                .collect(Collectors.toList());
    }


    @Override
    public List<LeftTopMainChartVO> getLeftTopMainChartData() {
        List<LeftTopMainChartVO> list = leftChartMapper.getLeftTopMainChartData();
        return list;
    }

    @Override
    public LeftCenterMainChatVO getLeftCenterMainChartData(Integer[] ages) {
        if (ages == null || ages.length == 0) {
            throw new NullDataException();
        }
        //预储数据
        //年龄按性别分布人数
        List<Integer> preMaleReserveList = leftChartMapper.getMalePreReserveByAge(ages);
        List<Integer> preFemaleReserveList = leftChartMapper.getFemalePreReserveByAge(ages);
        int maleTotal = preMaleReserveList.stream().mapToInt(Integer::intValue).sum(); // 求和
        int femaleTotal = preFemaleReserveList.stream().mapToInt(Integer::intValue).sum();
        int total = maleTotal + femaleTotal;
        LeftCenterMainChatVO.ReserveOrClub reserve = new LeftCenterMainChatVO.ReserveOrClub();
        reserve.setTotal(total);
        reserve.setAges(new LeftCenterMainChatVO.Ages(preMaleReserveList, preFemaleReserveList));
        reserve.setGenderCount(new LeftCenterMainChatVO.GenderCount(maleTotal, femaleTotal));

        //社团数据
        List<Integer> clubMaleReserveList = leftChartMapper.getClubMalePreReserveByAge(ages);
        List<Integer> clubFemaleReserveList = leftChartMapper.getClubFemalePreReserveByAge(ages);
        maleTotal = clubMaleReserveList.stream().mapToInt(Integer::intValue).sum(); // 求和
        femaleTotal = clubFemaleReserveList.stream().mapToInt(Integer::intValue).sum();
        total = maleTotal + femaleTotal;
        LeftCenterMainChatVO.ReserveOrClub club = new LeftCenterMainChatVO.ReserveOrClub();
        club.setTotal(total);
        club.setAges(new LeftCenterMainChatVO.Ages(clubMaleReserveList, clubFemaleReserveList));
        club.setGenderCount(new LeftCenterMainChatVO.GenderCount(maleTotal, femaleTotal));

        return new LeftCenterMainChatVO(reserve, club);
    }



    @Override
    public LeftBottomMainChartVO getLeftBottomMainChartData() {
        // 1. 获取男女饼图数据
        List<LeftBottomMainChartPieChartVO> maleAndFemaleDataLIst = leftChartMapper.getPieData();

        LeftBottomMainChartPieChartVO malePie = new LeftBottomMainChartPieChartVO();
        LeftBottomMainChartPieChartVO femalePie = new LeftBottomMainChartPieChartVO();

        maleAndFemaleDataLIst.forEach(pie -> {
            if(pie.getGender() == 1 ){
                BeanUtils.copyProperties(pie, malePie);
            }else {
                BeanUtils.copyProperties(pie, femalePie);
            }
        });

        // 2. 获取BMI区间柱状图数据
        List<Integer> maleBar = leftChartMapper.getBarData(1);
        List<Integer> femaleBar = leftChartMapper.getBarData(2);

        // 3. 获取身高折线图数据
        List<Integer> maleDataList = leftChartMapper.getLineChartData(1);
        List<Integer> femaleDataList = leftChartMapper.getLineChartData(2);

        LeftBottomMainChartVO.LineChart lineChart = new LeftBottomMainChartVO.LineChart(
                maleDataList, femaleDataList);

        return new LeftBottomMainChartVO(malePie, femalePie, maleBar, femaleBar, lineChart);
        }

    @Override
    public HeardVO getHeadData() {
        Integer totalClub = leftChartMapper.getTotalClub();
        Integer totalReserve = leftChartMapper.getTotalReserve();
        Integer totalJoinArmy = leftChartMapper.getTotalJoinArmy();
        return new HeardVO(totalClub, totalReserve, totalJoinArmy);
    }
}
